package com.uduemc.biso.core.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtil {

	final private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

	public static String post(String url, Map<String, String> valuePair) {
		String result = null;
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		List<BasicNameValuePair> list = new ArrayList<>();
		for (Map.Entry<String, String> entry : valuePair.entrySet()) {
			list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(list));
			CloseableHttpResponse response = client.execute(httpPost);
			// 判断状态码是否为200
			if (response.getStatusLine().getStatusCode() == 200) {
				// 返回响应体的内容
				result = EntityUtils.toString(response.getEntity(), "UTF-8");

				logger.info("远程获取临时 code 成功 response：" + result);
			} else {
				logger.error("远程获取临时 code 失败, status code：" + response.getStatusLine().getStatusCode());
			}
			client.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String get(String url, Map<String, String> valuePair) {
		String result = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		if (valuePair != null) {
			// 创建参数队列
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			for (Map.Entry<String, String> entry : valuePair.entrySet()) {
				params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}
			String paramsStr;
			try {
				paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, "UTF-8"));
				url = url + "?" + paramsStr;
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 创建httpget
		HttpGet httpget = new HttpGet(url);
		logger.info("executing request " + httpget.getURI());
		// 执行get请求.
		try {
			CloseableHttpResponse response = httpclient.execute(httpget);
			try {
				// 获取响应实体
				HttpEntity entity = response.getEntity();
				// 打印响应状态
				logger.info(response.getStatusLine().toString());
				result = EntityUtils.toString(response.getEntity(), "UTF-8");
				if (entity != null) {
					// 打印响应内容长度
					logger.info("Response content length: " + entity.getContentLength());
					// 打印响应内容
					logger.info("Response content: " + result);
				}

			} finally {
				response.close();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	public static String get(String url) {
		return get(url, null);
	}

}
